package com.intelligent.config;

import com.intelligent.security.AuthInterceptor;
import com.intelligent.security.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

        @Autowired
        private AuthorizationInterceptor authorizationInterceptor;

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new AuthInterceptor())
                                .addPathPatterns("/**")
                                .excludePathPatterns(
                                                "/login",
                                                "/login/send-code",
                                                "/login/phoneLogin",
                                                "/feign/**",
                                                "/error",
                                                "/actuator/**",
                                                "/swagger-ui/**",
                                                "/v3/api-docs/**",
                                                "/index.html",
                                                "/role-permission.html",
                                                "/role-permission/**",
                                                "/*.js",
                                                "/*.css",
                                                "/*.png",
                                                "/*.jpg",
                                                "/*.ico");
                registry.addInterceptor(authorizationInterceptor)
                                .addPathPatterns("/**")
                                .excludePathPatterns(
                                                "/login",
                                                "/login/send-code",
                                                "/login/phoneLogin",
                                                "/feign/**",
                                                "/error",
                                                "/actuator/**",
                                                "/swagger-ui/**",
                                                "/v3/api-docs/**",
                                                "/index.html",
                                                "/role-permission.html",
                                                "/role-permission/**",
                                                "/*.js",
                                                "/*.css",
                                                "/*.png",
                                                "/*.jpg",
                                                "/*.ico");
        }
        
        /**
         * 配置静态资源处理
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/**")
                        .addResourceLocations("classpath:/static/");
        }

        /**
         * 配置跨域支持
         */
        @Override
        public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOriginPatterns("*")
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                        .allowedHeaders("*")
                        .allowCredentials(true)
                        .maxAge(3600);
        }
}
